package cn.itcast.jobs;

import cn.itcast.constant.RedisConstant;
import cn.itcast.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Iterator;
import java.util.Set;

/**
 * @author: peterChen
 * @date: 2019/8/13 22:12
 * @description: TODO
 * @version: 1.0
 */

public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){
        //获取差异图片
        Set<String> strings = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        //遍历差异图片（以DB缓存为准，差异为垃圾图片）
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()){
            String pic = iterator.next();
            //删除七牛云中的垃圾图片
            QiniuUtils.deleteFileFromQiniu(pic);
            //清除上传文件缓存中的垃圾图片
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,pic);
        }
    }
}
